{extend name="extend/base" /}

{block name="header"}{include file="public/header" title="Telegram_Ai_Bot | 用户界面-自动加群" annotation="header钩子"}{/block}

{block name="title"}自动加群<h6 class="card-subtitle text-muted">请选择至少2个群，否做程序会出错</h6>{/block}
{block name="btn"}创建配置{/block}

{block name="config"}
<select class="form-control-sm" id="example" style="width:200px;margin-right:30px;">
    <option value=""><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">实例选择...</font></font></option>
    {volist name="examples" id="ep"}
    <option value="{$ep.id}"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">{$ep.name}</font></font></option>
    {/volist}
</select>
{/block}

{block name="btn"} <span class="badge badge-primary" onclick="get_config()"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">立即提交</font></font></span>{/block}

{block name="tg_qun"}
{volist name="tg_qun" id="vo" empty="暂无数据"}
<label class="custom-control custom-checkbox custom-control-inline">
    {eq name="vo['is_verify']" value="0"}
    <input type="checkbox" name="qun" id="qun{$vo.id}" onclick="quxiao({$vo.id},{$vo.is_verify})" value="{$vo.id}" class="custom-control-input">
    <span class="custom-control-label" data-toggle="tooltip" data-placement="top" title="" data-original-title="{$vo.name_cn}">
        <font style="vertical-align: inherit;">
            <font style="vertical-align: inherit;" onclick="jump_qun('{$vo.url}')">
                {$vo.name}
            </font>
        </font>
    </span>
    {else/}
    <input type="checkbox disabled" name="qun" id="qun{$vo.id}" onclick="quxiao({$vo.id},{$vo.is_verify})" value="{$vo.id}" class="custom-control-input">
    <span class="custom-control-label" data-toggle="tooltip" data-placement="top" title="" data-original-title="{$vo.name_cn}">
        <font style="vertical-align: inherit;">
            <font style="vertical-align: inherit;color:#CFCFCF;" onclick="jump_qun('{$vo.url}')">
                {$vo.name}
            </font>
            <font class="text-danger" style="vertical-align: inherit;">需要验证</font>
        </font>
    </span>
    {/eq}
</label>
{/volist}
{/block}

{block name="group_type"}
    <a href="/join_qun" class="badge  {$gtid == 0 ? 'badge-dark':'badge-light'}"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">全部</font></font></a>
    {volist name="group_type" id="gt"}
    <a href="/join_qun?gt_id={$gt.id}" class="badge {$gt.id == $gtid ? 'badge-dark':'badge-light'}"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">{$gt.name}</font></font></a>
    {/volist}
{/block}


{block name="title2"}配置列表{/block}

{block name="add_group"}
{volist name="join_qun_config" id="jqc" empty="$empty"}
<div class="col-xl-4 col-lg-4 col-md-6 col-sm-12 col-12">
    <div class="card">
        <div class="card-header d-flex">
            <h4 class="mb-0"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">实例名：{$jqc.name}</font></font></h4>
            <div class="dropdown ml-auto">
                <a class="toolbar" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <i class="fas fa-ellipsis-v"></i>  </a>
                <div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuLink" x-placement="bottom-end" style="position: absolute; transform: translate3d(18px, 23px, 0px); top: 0px; left: 0px; will-change: transform;">
                    <a class="dropdown-item" href="javascript:perform({$jqc.aid});"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">执行</font></font></a>
                    <a class="dropdown-item" href="javascript:delete_config({$jqc.aid})"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">删除</font></font></a>
                </div>
            </div>
        </div>
        <div class="card-body">
            {eq name="jqc['type']" value="0"}
            <p class="card-text" id="perform_type{$jqc.aid}">状态：<font class="text-danger" style="vertical-align: inherit;"><font style="vertical-align: inherit;">未执行</font></font></p>
            {else/}
            <p class="card-text">状态：<font class="text-success" style="vertical-align: inherit;"><font style="vertical-align: inherit;">已执行</font></font></p>
            {/eq}
            <p class="card-text">创建时间：{:date('Y-m-d H:i:s',$jqc.atime)}</p>
            <hr>
            <div>
                <div style="margin:-20px 0 0 -16px;height:20px;" class="" id="heading{$jqc.aid}">
                    <h5 class="mb-0">
                       <button class="btn btn-link collapsed" data-toggle="collapse" data-target="#collapse{$jqc.aid}" aria-expanded="false" aria-controls="collapse1">
                         <span class="fas fa-angle-down mr-3 text-dark"></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;" class="text-dark">等待加入
                       </font></font></button>
                      </h5>
                </div>
                <div id="collapse{$jqc.aid}" class="collapse" aria-labelledby="heading{$jqc.aid}" data-parent="#accordion3" style="">
                    <div class="card-body">
                        {volist name="jqc['datas']" id="jqcs" empty="数据为空"}
                            {eq name="jqcs['type']" value="0"}
                            <span class="badge badge-primary"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> {$jqcs.name}</font></font></span>
                            {/eq}
                        {/volist}
                    </div>
                </div>
            </div>
            <hr>
            <div>
                <div style="margin:-20px 0 0 -16px;height:20px;" id="headings{$jqc.aid}">
                    <h5 class="mb-0">
                       <button class="btn btn-link collapsed" data-toggle="collapse" data-target="#collapses{$jqc.aid}" aria-expanded="false" aria-controls="collapses{$jqc.aid}">
                         <span class="fas fa-angle-down mr-3 text-dark"></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;" class="text-dark">加入成功
                       </font></font></button>
                      </h5>
                </div>
                <div id="collapses{$jqc.aid}" class="collapse" aria-labelledby="headings{$jqc.aid}" data-parent="#accordion3" style="">
                    <div class="card-body">
                        {volist name="jqc['datas']" id="jqcss" empyt="数据为空"}
                            {eq name="jqcss['type']" value="1"}
                            <span class="badge badge-primary"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> {$jqcss.name}</font></font></span>
                            {/eq}
                        {/volist}
                    </div>
                </div>
            </div>
            <hr>            
        </div>
    </div>
</div>   
{/volist}                            
{/block}   


{block name="footer"}{include file="public/footer"  annotation="footer钩子"}{/block}

{block name="js"}
<script>
        let data = [];
        //提交
        function get_config(){
            //zhi
            var qun = $('input[name="qun"]');
            var thisLength = qun.length;
            var str = [];
            for(var i = 0;i<thisLength;i++){
                if(qun[i].checked == true){
                    str[i] = qun[i].value;
                }
            }
            
            for(var strs in str){
               // console.log(str[strs]);
                if(data.indexOf(str[strs]) == -1){
                    data.push(str[strs]);
                }
            }
            //console.log(JSON.stringify(data));
            
            var example = $('#example').val();
            if(!example){
                msg('提示框','请选择实例！');
                return;
            }
            
            if(data.length == 0){
                msg('提示框','请选择要加入的群！');
                return;                
            }
            $.post('/index/Api/get_config',{'example':example,'data':JSON.stringify(data)},function(res){
                if(res.code>0){
                    msg('提示框',res.msg);
                }else{
                    msg('提示框',res.msg);
                    setTimeout(function(){location.reload();},800);
                }
            },'json');
        }
        
        //全选
        function selectAll(){
            var isCheck = $('#quanxuan').is(':checked');
            console.log(isCheck);
            if(!isCheck){
                data = [];
            }
            $('input[type="checkbox"]').each(function(){
                this.checked = isCheck;
            });
        }
        
        //取消选择框
        function quxiao(id){
            var isCheck = $('#qun' + id).is(':checked');
            if(!isCheck){
                data.forEach(function(item,index,arr){
                    if(item == id){
                        arr.splice(index,1);
                    }
                });
            }
        }
        
        function jump_qun(url){
            window.open(url);
        }

</script>
<script>

    $('.collapse').on('shown.bs.collapse', function() {
        $(this).parent().find(".fa-angle-down").removeClass("fa-angle-down").addClass("fa-angle-up");
    }).on('hidden.bs.collapse', function() {
        $(this).parent().find(".fa-angle-up").removeClass("fa-angle-up").addClass("fa-angle-down");
    });

    $('.panel-heading a').click(function() {
        $('.panel-heading').removeClass('active');

        //If the panel was open and would be closed by this click, do not active it
        if (!$(this).closest('.panel').find('.panel-collapse').hasClass('in'))
            $(this).parents('.panel-heading').addClass('active');
    });
    

</script>
<script>
    let jump = 0;
    //执行配置
    function perform(id){
        $('#perform_type'+id).html('状态：<font class="text-primary" style="vertical-align: inherit;"><font style="vertical-align: inherit;" id="perform_type'+id+'"><span class="dashboard-spinner spinner-xs"></span>正在执行中...</font></font>');
        jump = 1;
        $.get('/index/Api/jiaqun',{'id':id},function(res){
            if(res.code>0){
                jump = 0;
                msg('提示框',res.msg);
            }else{
                jump = 0;
                msg('提示框',res.msg);
                $('#perform_type'+id).html('状态：<font class="text-success" style="vertical-align: inherit;"><font style="vertical-align: inherit;">已执行</font></font>')
                setTimeout(function(){location.reload();},800)
            }
        },'JSON');
    }
    
    //删除配置
    function delete_config(id){
        $.get('/index/Api/delete_config/id/'+id,function(res){
            if(res.code>0){
                msg('提示框',res.msg);
            }else{
                msg('提示框',res.msg);
                setTimeout(function(){location.reload();},800)
            }  
        },'json');
    }
    
    
    //执行自动加群时 禁止切换
    if(jump == 1){
        window.onbeforeunload = function (e) {
        e = e || window.event;
            
        // For IE and Firefox prior to version 4
        if (e) {
            e.returnValue = '是否离开此页面';
        }
        
        
        // For Safari
         return '是否离开此页面';
        };
    }
</script>

<style>
    form{margin-top:30px;}

</style>
{/block}

